package com.kolalo.comm;

import java.io.*;

/** Java实现eval（）方法，完成动态编译 https://blog.csdn.net/qq_39396275/article/details/77801143*/
public class EvalHelper {
	public static void eval(String str, boolean isExpression) {
		try {
			StringBuilder strB = new StringBuilder(80);
			strB.append("public class Hello {");
			strB.append("public static void main(String[] args) {");
			if (isExpression) {
				strB.append("System.out.println(");
				strB.append(str);
				strB.append(");");
			} else {
				strB.append(str);
			}
			strB.append("}}");
			// 2-将拼接好的字符串保存到一个Hello.java的文件里
			OutputStream out = new FileOutputStream("Hello.java");
			out.write(strB.toString().getBytes());
			out.close();
			// 3-调用java进程来编译Hello.java
			Process javacProcess = Runtime.getRuntime().exec("javac Hello.java");
			// 4-读取javac进程中的错误流信息
			InputStream error = javacProcess.getErrorStream();
			// 读取流中的数据
			byte[] b = new byte[1024];
			// 对每一个文件里的内容进行复制
			int len = -1;// 表示已经读取了多少个字节，如果len返回-1，表示已经读到最后
			while ((len = error.read(b)) != -1) {
				String msg = new String(b, 0, len, "gbk");
				System.out.print(msg);
			}
			// 关闭资源
			error.close();
			// 5-调用java进程来运行Hello.clss
			Process javaProcess = Runtime.getRuntime().exec("java Hello");
			// 6-读取java进程流中的信息
			InputStream info = javaProcess.getInputStream();
			while ((len = info.read(b)) != -1) {
				String msg = new String(b, 0, len, "gbk");
				System.out.print(msg);
			}
			info.close();
			// 7-删除java和class文件
			new File("Hello.java").delete();
			new File("Hello.class").delete();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}
}
